/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef _PREVIEW_H
#define _PREVIEW_H

#include <iostream>
#include <fstream>
#include <sstream>
#include <limits>

#include <QGLWidget>
#include <QtOpenGL>
#include <QColor>
#include <QTimer>
#include <QMouseEvent>
#include <QPointF>
#include <QRectF>
#include <QResizeEvent>
#include <QKeyEvent>

#include <cmath>

#include "../../reader.h"
#include "../../lexical.h"
#include "../../syntax.h"
#include "../../gType.h"
#include "../../serialize.h"

class preview: public QGLWidget{

	Q_OBJECT
	
	public:
		enum types{NORMAL, BUFF};
	
	private:
		static const float INC = 0.1;
		static const float BORDER = 2.0;
		
		QTimer *timer;
		QPointF angle, clicked, clickedOld;
		QList<float> middle;
		gObjectContainer *buffCont;
		gObject object;
		float a;
		bool click;
		GLuint listNormal, listBuff;
		bool showNormal, showBuff, showAxis, showPoints;
	
		void swap(float *, float *) const;
		
		void getRect(QList<float> &, gObject &) const;
		void getRect(QList<float> &, gObjectContainer &) const;
		void getMiddle(const QList<float> &, QList<float> &) const;
		float longestEdge(const QList<float> &) const;
		void setCamera(const QList<float> &) const;
		void drawAxis();
		void drawBegin(gTypeGo &);
		void drawEnd(gTypeGo &);
		void drawObject(gObject &, types);
		void drawContainer(gObjectContainer &, types);
		void initObject(QString &, gObjectContainer *);
		void initCamera();
		void fixRotatePoint();
		void drawCircle(gTypeGo &, gTypeGoCircle &);
		void drawLine(gTypeGo &, gTypeGo &);
		
	private slots:
		void timeout();
	
	protected:
		virtual void initializeGL();
		virtual void resizeGL(int, int);
		virtual void paintGL();
		virtual void mouseMoveEvent(QMouseEvent *);
		virtual void mousePressEvent(QMouseEvent *);
		virtual void mouseReleaseEvent(QMouseEvent *);
		virtual void keyPressEvent(QKeyEvent *);
	
	public:
		preview(QWidget *, QGLWidget *, QString &, gObjectContainer *, gObjectContainer *);
		
		void setFile(QString &);

};

#endif
